package com.changgou.utils;

import com.sun.management.OperatingSystemMXBean;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;



// 获取服务器信息
public class SystemDeployPropertiesRedisUtils {
    // window
    public static final  String SYSTEM_TYPE_WINDOWS = "windows";
    // linux
    public static final String SYSTEM_TYPE_LINUX = "linux";

    // 获取服务器内存使用量
    public static  long[] memery(){ return  getMemery(); }

    // 获取windows中内存
    private static long[] getMemery() {
        OperatingSystemMXBean osmb = (com.sun.management.OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean();
        // 获取物理内存
        long totalPhysicalMemorySize = osmb.getTotalPhysicalMemorySize();
        // 获取剩余内存
        long freePhysicalMemorySize = osmb.getFreePhysicalMemorySize();

        // 计算内存使用百分比
        Double radio = (Double)(1 - freePhysicalMemorySize * 1.0 / totalPhysicalMemorySize) * 100;
        return new long[]{totalPhysicalMemorySize,freePhysicalMemorySize};
    }
    // 获取服务器磁盘使用情况
    public static long[] disk(){
        String property = System.getProperties().getProperty("os.name").toLowerCase();
        if (property.contains(SYSTEM_TYPE_WINDOWS)) {
            return  getDiskOnWindows();
        }else if(property.contains(SYSTEM_TYPE_LINUX)){
            return getDiskOnLinux();
        }
        return null;

    }

    private static long[] getDiskOnLinux() {
       long[] linuxDesk = new long[2];
       try {

           Runtime runtime = Runtime.getRuntime();
           Process exec = runtime.exec("df -hl"); // 执行命令
           BufferedReader in = null;

           try {
               in = new BufferedReader(new InputStreamReader(exec.getInputStream()));
               String str = null;
               String[] strArray = null;
               int line = 0;
               while ( (str = in.readLine()) !=null ){
                   line ++ ;
                   if(line != 2){
                       continue;
                   }
                   int m = 0;
                   String[] split = str.split("");
                   for (String tmp: split) {
                       if(tmp.trim().length() == 0){
                           continue;
                       }
                       ++m;
                       if(tmp.endsWith("G") || tmp.endsWith("Gi")){
                           // 目前服务器
                           if(m == 2){
                               linuxDesk[1] = Long.valueOf(tmp.substring(0,tmp.indexOf("G")));
                           }
                           if(m == 3){
                               linuxDesk[0] = Long.valueOf(tmp.substring(0,tmp.indexOf("G")));
                           }
                       }
                   }

               }
           }catch (Exception e){
               e.printStackTrace();
           }finally {
               in.close();
           }

       }catch (Exception e){
              e.printStackTrace();
       }
       return linuxDesk;
    }

    private static long[] getDiskOnWindows() {
        long total = 0L;
        long free = 0L;
        for (char i = 'A'; i <= 'Z'; i++) {
            String dirName = i + ":/";
            File win = new File(dirName);
            if(win.exists()){
                total += win.getTotalSpace();
                free += win.getFreeSpace();
            }
        }
        Double compare = (1 - free * 1.0 / total) * 100;
        return  new long[]{total,free};
    }

}
